<template>
  <div>
    <h2>音乐列表：</h2>
    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column type="index" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column prop="_id" label="音乐ID" align="center">
      </el-table-column>
      <el-table-column prop="title" label="音乐名称" align="center">
      </el-table-column>
      <el-table-column prop="artist" label="演唱者" align="center">
      </el-table-column>
      <el-table-column prop="created_time" label="添加时间" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button type="text" @click="$router.push(`/musicEdit/${row._id}`)"
            >修改</el-button
          >
          <el-button type="text" @click="deleteMusic(row._id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-box">
      <el-pagination
        @current-change="handleCurrentChange"
        background
        layout="total, prev, pager, next"
        :total="this.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getMusiclist, getMusicdelete } from "@/api/index.js";
export default {
  data() {
    return {
      loading: true,
      tableData: [],
      page: 1,
      total: 0,
    };
  },
  mounted() {
    this.getMusiclist();
  },
  methods: {
    getMusiclist() {
      getMusiclist().then((res) => {
        this.tableData = res.data.items;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    handleCurrentChange(val) {
      this.page = val;
      this.axios.get(`/rest/music?page=${this.page}&size=${10}`).then((res) => {
        this.tableData = res.data.items;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    deleteMusic(id) {
      this.$confirm("此操作将永久删除该音乐, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          getMusicdelete(id).then((res) => {
            console.log(res);
            if (res.data.status == 200) {
              this.$message.success(res.data.success);
              this.$router.push("/musicList");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style lang="less" scoped></style>
